package casia.isiteam.springbootshiro.service.event;

import casia.isiteam.datagroup.es.common.FieldOccurs;
import casia.isiteam.datagroup.es.operation.search.EsIndexSearch;
import casia.isiteam.springbootshiro.model.po.common.Institution;
import casia.isiteam.springbootshiro.model.po.common.InstitutionRepository;
import casia.isiteam.springbootshiro.properties.other.IndexConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author wzy
 * Date 2018/1/15 16:49
 */
@Service
public class EventService {
    @Resource
    private InstitutionRepository institutionRepository;

    @Transactional(value = "transactionManagerCommon",rollbackFor=Exception.class) //开启事务
    public Institution saveInstitution(Institution institution) throws Exception{
        Institution rs = institutionRepository.save( institution );
        return rs;
    }
    public List queryDepByInstitution(int inst_id) throws Exception{
        List rs = institutionRepository.queryDup(inst_id);
        return rs;
    }
    public Institution finadInstitution(Institution institution) throws Exception{
        Institution rs = institutionRepository.save( institution );
        return rs;
    }
    public Object queryByEidGroupMessge(Integer eid,Integer pageNo, Integer pageSize){
        EsIndexSearch search= IndexConfig.getEsIndexSearch("event_data_extract_result_v","analysis_data");
        JSONObject json =new JSONObject();

        search.reset();
        search.addPhraseQuery("eid",""+eid, FieldOccurs.MUST);
        search.setStart( (pageNo-1)*pageSize );
        search.setRow(pageSize);
        String[] fields = new String[]{"content","pubtime","uid","city"};
        search.execute(fields);
        List<String[]> resultLists = search.getResults();

        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
        for (String[] strings : resultLists) {
            short i = 0;
            Map<String, String> map = new HashMap<>();
            for (String s : strings) {
                map.put(fields[i++], s);
            }
            dataList.add(map);
        }

        json.put("total",search.getTotal());
        json.put("info", dataList );
        return json;
    }
}
